package com.toogu.mazhangmen.util;

import android.app.Activity;
import android.os.Message;
import android.util.Log;

import com.basic.G;
import com.google.gson.Gson;
import com.lib.MsgContent;
import com.lib.sdk.struct.SDBDeviceInfo;
import com.manager.account.AccountManager;
import com.manager.account.BaseAccountManager;
import com.manager.db.XMDevInfo;
import com.toogu.mazhangmen.bean.EquipmentsBean;
import com.toogu.mazhangmen.bean.ProjectBean;
import com.toogu.mazhangmen.bean.ProjectListBean;
import com.toogu.mazhangmen.bean.SockReturnBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class EquipmentUtil {
    public interface GetCallback{
        void onFailure(String message);
        void onSuccess();
    }
    private static ProjectListBean projectListBean;
    private static List<EquipmentsBean> equipmentsBeanList;

    public static final int Door=-1;
    public static final int Help=-2;
    public static final int OutLine=-3;
    public static final int Search=-4;

    private static int MODE = 0;

    private static Map<String,Boolean> isHelpMap = new HashMap<>();

    public EquipmentUtil(){
        equipmentsBeanList = new ArrayList<>();
    }

    public static boolean isHelp(String devId){
        if(isHelpMap.containsKey(devId)) return isHelpMap.get(devId);
        return false;
    }

    public static EquipmentsBean setHelp(SockReturnBean sockReturnBean){
        if(projectListBean!=null){
            for(ProjectBean dataBean : projectListBean.getData()){
                if(sockReturnBean.getParkid().equals(dataBean.getId()+"")){
                    for(EquipmentsBean equipmentsBean :dataBean.getEquipments()){
                        for(EquipmentsBean.DoorBean doorBean: equipmentsBean.getDoors()){
                            if(sockReturnBean.getDoorid().equals(doorBean.getId())){
                                isHelpMap.put(equipmentsBean.getCode(),true);
                                return equipmentsBean;
                            }
                        }
                    }
                }
            }
        }
        return null;
    }

    public static void closeHelp(String devId){
        isHelpMap.put(devId,false);
    }

//    public void getNewDate(Activity activity,GetCallback getCallback){
//        NetWorkUtil.getNetWorkUtil().getProject(activity, "1", new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                getCallback.onFailure(e.getMessage());
//            }
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String responseText = response.body().string();
//                if (response.isSuccessful()) {
//                    Log.d("getProject", "onResponse: " + responseText);
//                    ProjectBean projectBean = new Gson().fromJson(responseText, ProjectBean.class);
//                    projectListBean=new ProjectListBean();
//                    projectListBean.setData(Collections.singletonList(projectBean));
//                    for (ProjectBean dataBean : projectListBean.getData()) {
//                        for(EquipmentsBean equipmentsBean :dataBean.getEquipments()){
//                            addDev(equipmentsBean);
//                            moveDoor(equipmentsBean);
//                        }
//                    }
//                    getCallback.onSuccess();
//                }
//                else{
//                    getCallback.onFailure(responseText);
//                }
//            }
//        });
//    }


    public void getNewDate(Activity activity,GetCallback getCallback){
        NetWorkUtil.getNetWorkUtil().getProjectList(activity, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                getCallback.onFailure(e.getMessage());
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseText = response.body().string();
                if (response.isSuccessful()) {
                    Log.d("getDoorProjectList", "onResponse: " + responseText);
                    projectListBean = new Gson().fromJson(responseText, ProjectListBean.class);
                    for (ProjectBean dataBean : projectListBean.getData()) {
                        for(EquipmentsBean equipmentsBean :dataBean.getEquipments()){
                            addDev(equipmentsBean);
                            moveDoor(equipmentsBean);
                        }
                    }
                    getCallback.onSuccess();
                }
                else{
                    getCallback.onFailure(responseText);
                }
            }
        });
    }

    public String[] getProjectListName(){
        int size = projectListBean.getData().size();
        String[] projectName = new String[size];
        for(int i=0;i<size;i++){
            projectName[i]=projectListBean.getData().get(i).getName();
        }
        return projectName;
    }

    public List<EquipmentsBean> getAll(){
        equipmentsBeanList.clear();
        for (ProjectBean dataBean : projectListBean.getData()) {
            equipmentsBeanList.addAll(dataBean.getEquipments());
        }
        return equipmentsBeanList;
    }

    public List<EquipmentsBean> getHelp(){
        equipmentsBeanList.clear();
        for (ProjectBean dataBean : projectListBean.getData()) {
            for(EquipmentsBean equipmentsBean :dataBean.getEquipments()){
                if(isHelp(equipmentsBean.getCode())) {
                    equipmentsBeanList.add(equipmentsBean);
                }
            }
        }
        return equipmentsBeanList;
    }

    public List<EquipmentsBean> getOutLine(){
        equipmentsBeanList.clear();
        for (ProjectBean dataBean : projectListBean.getData()) {
            for(EquipmentsBean equipmentsBean :dataBean.getEquipments()){
                if(!equipmentsBean.getStatus().equals("1")) {
                    equipmentsBeanList.add(equipmentsBean);
                }
            }
        }
        return equipmentsBeanList;
    }

    public List<EquipmentsBean> getProject(int position){
        position--;//除去 请选择选项
        equipmentsBeanList.clear();
        equipmentsBeanList.addAll(projectListBean.getData().get(position).getEquipments());
        return equipmentsBeanList;
    }

    public List<EquipmentsBean> getSearch(String search){
        equipmentsBeanList.clear();
        String[] temp = search.split(" ");
        int isTrue=1;
        for (ProjectBean dataBean : projectListBean.getData()) {
            for(EquipmentsBean equipmentsBean :dataBean.getEquipments()){
                isTrue=1;
                for(String str:temp){
                    if(!(equipmentsBean.getName().contains(str)||dataBean.getName().contains(str))) {
                        isTrue=0;
                        break;
                    }
                }
                if(isTrue==1) equipmentsBeanList.add(equipmentsBean);
            }
        }
        return equipmentsBeanList;
    }

    private void addDev(EquipmentsBean equipmentsBean){
        SDBDeviceInfo deviceInfo = new SDBDeviceInfo();
        G.SetValue(deviceInfo.st_0_Devmac,equipmentsBean.getCode());
        G.SetValue(deviceInfo.st_4_loginName,equipmentsBean.getUsername());
        G.SetValue(deviceInfo.st_5_loginPsw,equipmentsBean.getPassword());
        XMDevInfo xmDevInfo = new XMDevInfo();
        xmDevInfo.sdbDevInfoToXMDevInfo(deviceInfo);
        AccountManager.getInstance().addDev(xmDevInfo, new BaseAccountManager.OnAccountManagerListener() {
            @Override
            public void onSuccess(int i) {
            }

            @Override
            public void onFailed(int i, int i1) {
            }

            @Override
            public void onFunSDKResult(Message message, MsgContent msgContent) {
            }
        });
    }

    public static void moveDoor(EquipmentsBean equipmentsBean) {
        if (equipmentsBean.getDoors() != null&&equipmentsBean.getDoors().size() == 1) {
            equipmentsBean.setDoor(equipmentsBean.getDoors().get(0));
            equipmentsBean.setDoors(null);
        }
    }
}

